//
//  CollectManager.m
//  Hero
//
//  Created by lanou3g on 16/5/9.
//  Copyright © 2016年 刘永庆. All rights reserved.
//

#import "CollectManager.h"
#import "YTKKeyValueStore.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import "ConsultModel.h"
#import "UserInfoManager.h"
#import "PicModel.h"
@interface CollectManager ()
@property (nonatomic, strong)YTKKeyValueStore *myStory;
@property (nonatomic, strong) NSString *uid;
@end
@implementation CollectManager
#warning ----应该用基类model存储-----比较简单-----
- (BOOL)isColllectWith:(NSString *)url
{
   NSArray *array = [self selectAllArticl];
    for (ConsultModel *model in array) {
        while ([model.article_url isEqualToString:url]) {
//             NSLog(@"you");
            return YES;
        }
    }
//     NSLog(@"meiyou");
    return NO;
}
+ (instancetype)shareManager
{
    static CollectManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[CollectManager alloc]init];
    });
    return manager;
}
- (NSString *)uid
{
    if (!_uid) {
        _uid =[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"auth"]];
    }
    return _uid;
}
- (YTKKeyValueStore *)myStory
{
    if (!_myStory) {
        _myStory = [[YTKKeyValueStore alloc]initDBWithName:@"YQ.sqlite"];
    }
    return _myStory;
}
//收藏
- (void)collectArticle:(ConsultModel *)model
{
    [self.myStory createTableWithName:self.uid];
   NSDictionary *dic =  [self dictionaryFromModel:model];
    [self.myStory putObject:dic withId:model.article_id intoTable:self.uid];
//     NSLog(@"收藏成功");
}
//- (void)collectPicture:(PicModel *)model
//{
//    [self.myStory createTableWithName:self.uid];
//    NSDictionary *dic =  [self dictionaryFromModel2:model];
//    [self.myStory putObject:dic withId:model.ID intoTable:self.uid];
//    NSLog(@"收藏成功");
//}
//取消收藏
- (void)removeCollectArticleWith:(ConsultModel *)model
{
    [self.myStory createTableWithName:self.uid];
    [self.myStory deleteObjectById:model.article_id fromTable:self.uid];
    
}
//- (void)removePicture:(PicModel *)model
//{
//    [self.myStory createTableWithName:self.uid];
//    [self.myStory deleteObjectById:model.ID fromTable:self.uid];
//}
- (NSArray *)selectAllArticl
{
//    [self.myStory createTableWithName:@"wo"];
    [self.myStory createTableWithName:self.uid];
    NSArray *items = [self.myStory getAllItemsFromTable:self.uid];
    NSMutableArray *array = [NSMutableArray new];
    for (YTKKeyValueItem *item in items) {
        ConsultModel *model = [self modelFromDictionary:item.itemObject];
        [array addObject:model];
    }
    return array;
}

- (NSString *)getCollectTimeWith:(ConsultModel *)model
{
    YTKKeyValueItem *item = [self.myStory getYTKKeyValueItemById:model.article_id fromTable:self.uid];
    NSDate *date = item.createdTime;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strDate = [dateFormatter stringFromDate:date];
    return strDate;
}
//相互转化
-(NSDictionary *)dictionaryFromModel:(ConsultModel *)model
{
    unsigned int count = 0;
    Ivar *ivarArray = class_copyIvarList([model class], &count);
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (int i = 0; i < count; i ++) {
        NSString *keys = [NSString stringWithUTF8String:ivar_getName(ivarArray[i])];
        [array addObject:keys];
    }
    return [model dictionaryWithValuesForKeys:array];
}
- (ConsultModel *)modelFromDictionary:(NSDictionary *)dic
{
    ConsultModel *model = [ConsultModel new];
    [model setValuesForKeysWithDictionary:dic];
    return model;
}
-(NSDictionary *)dictionaryFromModel2:(PicModel *)model
{
    unsigned int count = 0;
    Ivar *ivarArray = class_copyIvarList([model class], &count);
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (int i = 0; i < count; i ++) {
        NSString *keys = [NSString stringWithUTF8String:ivar_getName(ivarArray[i])];
        [array addObject:keys];
    }
    return [model dictionaryWithValuesForKeys:array];
}
- (PicModel *)modelFromDictionary2:(NSDictionary *)dic
{
    PicModel *model = [PicModel new];
    [model setValuesForKeysWithDictionary:dic];
    return model;
}

@end
